﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Nage.Algorithms;
using Nage.Algorithms.Population;
using Nage.Algorithms.Solution;
using System.Collections.ObjectModel;
using Nage.Examples.Rastrigin.Solution;

namespace Nage.Examples.Rastrigin.Population
{
    public class CoordinatesPopulation : Collection<Coordinates>, IPopulation<Coordinates>
    {
        #region Private fields
        private IDictionary<Coordinates, ISolutionEvaluator<Coordinates>> _evaluatorDict;
        #endregion

        #region Public properties
        public ICollection<ISolutionEvaluator<Coordinates>> EvaluationList
        {
            get
            {
                return _evaluatorDict.Values;
            }
        }

        public bool IsEmpty
        {
            get
            {
                return this.Count == 0;
            }
        }
        #endregion

        #region Constructor
        public CoordinatesPopulation()
        {
            _evaluatorDict = new Dictionary<Coordinates, ISolutionEvaluator<Coordinates>>();
        }
        #endregion

        #region GetEvaluation
        ISolutionEvaluator<Coordinates> IPopulation<Coordinates>.GetEvaluation(Coordinates solution)
        {
            return _evaluatorDict[solution];
        }
        #endregion

        #region SetEvaluation
        public void SetEvaluation(Coordinates solution, ISolutionEvaluator<Coordinates> evaluation)
        {
            _evaluatorDict[solution] = evaluation;
        } 
        #endregion

        #region SetAllEvaluations
        public void SetAllEvaluations(ISolutionEvaluator<Coordinates> evaluation)
        {
            foreach (Coordinates coords in this)
            {
                SetEvaluation(coords, evaluation);
            }
        } 
        #endregion

        #region AsDictionary
        public IDictionary<Coordinates, ISolutionEvaluator<Coordinates>> AsDictionary()
        {
            return _evaluatorDict;
        } 
        #endregion

        #region ToString
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            foreach (Coordinates coords in this)
            {
                sb.AppendLine(coords.ToString());
            }
            return sb.ToString();
        } 
        #endregion

    }
}
